﻿Imports lbGlobalcom
Public Class fUserYRoles
    Private Sub CargaUsuarios()
        Try
            lstUsuarios.DataSource = cUsuariosOracle.GetUsuarios()
            lstUsuarios.DisplayMember = "UserName"
            lstUsuarios.ValueMember = "UserName"
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try

    End Sub
    Private Sub CargarRoles()
        Try
            lstRoles.DataSource = cUsuariosOracle.GetRoles()
            lstRoles.DisplayMember = "ROLE"
            lstRoles.ValueMember = "ROLE"
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
    Private Sub btnAddRole_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddRole.Click
        If txtRole.Text.Trim.Length > 0 Then
            Try
                cUsuariosOracle.CreateRol(txtRole.Text)
                CargarRoles()
                txtRole.Text = ""
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
        End If
    End Sub

    Private Sub fRoles_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            If Not CustomPrincipal.IsInRole("ADMINGC") Then
                MessageBox.Show("No tiene privilegios para ingresar a esta opcion, verifique con el administrador", "Permiso Denegado", MessageBoxButtons.OK, MessageBoxIcon.Stop)
                Me.Close()
            End If
            CargarRoles()
            CargaUsuarios()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
    Private Sub CargaRolesByUser()
        Try
            If Not TypeOf lstUsuarios.SelectedValue Is DataRowView Then
                lstRolesByUser.DataSource = cUsuariosOracle.GetRolesByUser(lstUsuarios.SelectedValue)
                lstRolesByUser.DisplayMember = "GRANTED_ROLE"
                lstRolesByUser.ValueMember = "GRANTED_ROLE"
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
    Private Sub lstUsuarios_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstUsuarios.SelectedIndexChanged
        CargaRolesByUser()
    End Sub

    Private Sub btnAddRolToUser_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddRolToUser.Click
        Try
            If lstRoles.SelectedValue IsNot Nothing AndAlso lstUsuarios.SelectedValue IsNot Nothing Then
                cUsuariosOracle.AddRoleToUser(lstUsuarios.SelectedValue, lstRoles.SelectedValue)
                CargaRolesByUser()
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub btnAddUser_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddUser.Click
        Try
            If txtUsuario.Text.Trim.Length > 0 Then
                cUsuariosOracle.CreateUser(txtUsuario.Text, txtPassword.Text)
                CargaUsuarios()
                txtUsuario.Text = ""
                txtPassword.Text = ""
                txtConfirmaPass.Text = ""
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub txtConfirmaPass_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtConfirmaPass.TextChanged
        If txtConfirmaPass.Text = txtPassword.Text Then
            btnAddUser.Enabled = True
        Else
            btnAddUser.Enabled = False
        End If
    End Sub

    Private Sub ToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripMenuItem1.Click
        Try
            If lstUsuarios.SelectedValue IsNot Nothing Then
                cUsuariosOracle.EliminaUser(lstUsuarios.SelectedValue)
                CargaUsuarios()
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub EliminaRolDeUsuarioToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EliminaRolDeUsuarioToolStripMenuItem.Click
        Try
            If lstRolesByUser.SelectedValue IsNot Nothing Then
                cUsuariosOracle.DelRoleToUser(lstUsuarios.SelectedValue, lstRolesByUser.SelectedValue)
                CargaRolesByUser()
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
End Class